package wangjg.coco.common;

import java.io.File;
import java.io.FileFilter;

/**
 * 文件过滤器
 * @author wangjg
 */
public class MyFileFilter implements FileFilter {
	private boolean allowHidden;
	private boolean allowFolder;
	private String[] extNames;
	
	private String getType(File file) {
		String fileName = file.getName();
		int idx = (fileName == null ? -1 : fileName.lastIndexOf('.'));
		if (idx > -1) {
			return fileName.substring(idx + 1);
		}
		return "";
	}
	
	private boolean checkExt(String extName){
		for(String ext:extNames){
			if(extName.equals(ext)){
				return true;
			}
		}
		return false;
	}

	public boolean accept(File file) {
		if (file.isHidden()) {
			return false;
		}
		if(!this.allowFolder && file.isDirectory()){
			return false;
		}
		if(!this.allowHidden && file.isHidden()){
			return false;
		}
		return this.checkExt(this.getType(file));
	}
}
